import { ref, onUnmounted } from 'vue';

export const useTimer = () => {
  const timer = ref(Date.now());
  let t = setInterval(() => {
    timer.value = Date.now();
  }, 1000);
  onUnmounted(() => {
    clearInterval(t);
  });
  return timer;
};

export const isExpired = (item, $now) => {
  if (!item) {
    return false;
  }
  if (item.foreverFlag) {
    return true;
  }
  if (item.startTime) item.startTimestamp = Date.valueOf(item.startTime).getTime();
  if (item.expireTime) item.expireTimestamp = Date.valueOf(item.expireTime).getTime();
  if (item.startTimestamp && item.startTimestamp > $now) {
    return false;
  }
  if (item.expireTimestamp && item.expireTimestamp < $now) {
    return false;
  }
  return true;
};
